package com.szholly.pro.watergas_server.ps.controller;

import java.util.ArrayList;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.szholly.data.general.*;
import com.szholly.plug.form.excel.utils.StringUtil;
import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.util.JsonUtil;
import com.szholly.pro.watergas_server.common.util.RedisDbExecute;
import com.szholly.pro.watergas_server.common.util.StationOnlineCompare;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import com.szholly.pro.watergas_server.ps.service.OnlineMonitorService;
import com.szholly.pro.watergas_server.ps.service.RemoteService;
import com.szholly.utils.spring.SpringBeanFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/OnlineMonitor/*")
public class OnlineMonitorController extends BaseController {
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	RemoteService remoteService;
	
	@Autowired
	OnlineMonitorService onlineMonitorService;
	
	@RequestMapping(value = "/getWsclcModel")
	public void getWsclcModel(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String sql = " select t.*,t.name as SDENAME from BC_ENTERPRISE_ATTR t where exp_no = @wwtpid ";
		String wwtpid = RequestUnit.getString("wwtpid");
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		GlobalParameter parm = new GlobalParameter("wwtpid", wwtpid);
		parms.add(parm);
		String result = DataUtil.getPageSelectJson(sql, parms);
		outString(result);
	}

	@RequestMapping(value = "/wsclc")
	public void statistic(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String sql = getCompanyListsql(resq);
		List<GlobalParameter> parm = new ArrayList<GlobalParameter>();
		List<RowBase> rows = DataUtil.getSelectRows(sql, parm);
		
		for(RowBase row : rows){
			String mnsStr = row.getSafeString("MNS");
			if(mnsStr.trim().equals("")){
				continue;
			}
			
			String[] mns = mnsStr.split(",");
			String onLine0="0";
			String onLine1="0";
			
			for(String mn : mns){
				String mnStr = mn.replace("ft0_mn", "").replace("ft1_mn", "");
				String channelid = RedisDbExecute.getString("stationInfo:"+mnStr+":deviceInfo:channelId");
				String onResult = "0";
				if(channelid != null && !channelid.trim().equals("")){
					onResult = "1";
				}
				if(mn.startsWith("ft0")){
					onLine0 = onResult;
				}
				if(mn.startsWith("ft1")){
					onLine1 = onResult;
				}
			}
			row.setValue("STATIONINFO","ft0_s"+onLine0+",ft1_s"+onLine1);
		}
		Collections.sort(rows, new StationOnlineCompare());
		String result = DataUtil.listToJSONObject(rows, "BC_ENTERPRISE_ATTR").toString();
		outString(result);
	}
	
	@RequestMapping(value = "/wsclcDetail")
	public void wsclcDetail(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String sql = "select t2.name as SDENAME,t2.OBJECTID as SDEOBJECTID,t2.Exp_No as SDEExp_No,t3.name as OPERATORNAME ,t2.* from BC_ENTERPRISE_ATTR t2  "
				+ " left join OPERATIONS_UNIT t3 on t2.OPERATOR=t3.guid "
				+" left join WWTP_ATTENTION t4 on t2.exp_no=t4.wwtp_exp_no "
				//+" left join DATA_DEFULATDOMAIN d2 on ( d2.S_DOMAINNAME = 'ATTENTION_LEVEL' AND d2.S_DOMAINCODE = t4.type )"
				+ " where 1=1 ";
		String Exp_No = resq.getParameter("Exp_No");
		if (Exp_No != null && !Exp_No.equals("")) {
			sql = sql + " and t2.Exp_No = '" + Exp_No + "'";
		}
		List<GlobalParameter> parm = new ArrayList<GlobalParameter>();
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("BC_ENTERPRISE_ATTR");
		String result = DataUtil.getPageSelectJson(sql, parm, pTable);
		outString(result);
	}
	
	public String getCompanyListsql(HttpServletRequest resq) {
		String sql = "select t.exp_no SDEEXP_NO,\n"
						+ "	t.name as SDENAME,t.temp_img,\n"
						+ "	t.img,wmsys.wm_concat(t.stationInfo) stationInfo,\n"
						+ "	wmsys.wm_concat(scs) scs,\n"
						+ " sum(wwtpStatus) wwtpStatus,\n"
						+ "	wmsys.wm_concat(mns) mns from (\n" +
						"  select w.exp_no,w.name,flow_type,w.img,w.temp_img,\n" + 
						" 'ft' || to_char(ms.flow_type) || '_s' || to_char(dc.status) stationInfo,\n"+
						" 'ft' || to_char(ms.flow_type) || '_mn' || to_char(dc.DEVICE_NUM) mns,\n"+
						" 'ft' || to_char(ms.flow_type) || '_sc' || to_char(dc.SITE_ID) scs,\n"+
						"  w.order_num, \n"+
						"  case to_number(dc.status) when null then 0 else to_number(dc.status) end wwtpStatus \n"+
						"  from BC_ENTERPRISE_ATTR w \n" + 
						"         left join BC_SITE_INFO ms on w.exp_no = ms.wwtp_id\n" + 
						"         left join BC_COLLECTION_INFO dc on ms.exp_no = dc.site_id\n" +
						"         WHERE 1=1 ";

		String name = resq.getParameter("NAME");
		String Exp_No = resq.getParameter("Exp_No");
		String pollType = resq.getParameter("pollType");
		if (name != null && !name.equals("")) {
			sql = sql + " and w.NAME like '%" + name + "%'";
		}
		if (Exp_No != null && !Exp_No.equals("")) {
			sql = sql + " and w.Exp_No = '" + Exp_No + "'";
		}
		if (pollType != null && !pollType.equals("")) {
			sql = sql + " and w.poll_type = '" + pollType + "'";
		}
		sql=sql+" order by w.objectid";
		sql += "       ) t\n" + 
				"       group by t.exp_no,t.name,t.img,t.order_num,t.temp_img order by t.order_num asc ";
		sql = " SELECT * FROM ( " + sql + ") t order by t.wwtpStatus desc nulls last ";
		return sql;
	}

	// 获取污水处理厂所属监测站点
	@RequestMapping(value = "/station")
	public void station(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String,String> params = getStationQueryParams();
		
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("MONITOR_STATION");
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = onlineMonitorService.queryStation(params, pTable, null);
		Collections.sort(list, new StationOnlineCompare());
		String result = DataUtil.listToJSONObject(list, null, pageInfo).toString();
		outString(result);
	}

	/**
	 * 查询站点坐标数据
	 */
	@RequestMapping(value = "/getStationListWithSpatial")
	public void getStationWithSpatial(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Date d1 = new Date();
		Map<String,String> params = getStationQueryParams();
		List<RowBase> list = onlineMonitorService.queryStationWithSpatial(params, null, null);
		Collections.sort(list, new StationOnlineCompare());
		
		JSONObject json = DataUtil.listToJSONObject(list, null, null);
		outString(json.toString());
		Date d2 = new Date();
		double span = (d2.getTime()-d1.getTime())/1000.0;
		logger.info("ST_AS_TEXT查询耗时： "+span+" 秒");
	}

	private Map<String,String> getStationQueryParams() {
		Map<String,String> params = new HashMap<>();
		String wwtp_id = RequestUnit.getParameter("WWTP_ID");
		String flow_type = RequestUnit.getParameter("FLOW_TYPE");
		String water_type = RequestUnit.getParameter("WATER_TYPE");
		String station_id = RequestUnit.getParameter("STATION_ID");
		String build_status = RequestUnit.getParameter("BUILD_STATUS");
		String station_name = RequestUnit.getParameter("STATION_NAME");
		String station_id_like = RequestUnit.getParameter("STATION_ID_LIKE");
		String companyCode = RequestUnit.getParameter("companyCode");
		String stationStatus = RequestUnit.getString("stationStatus");
		String s_domaincode_like = RequestUnit.getParameter("S_DOMAINCODE_LIKE");

		if (StringUtil.IsNotNullOrEmpty(wwtp_id)) {
			params.put("wwtp_id", wwtp_id);
		}
		if (StringUtil.IsNotNullOrEmpty(flow_type)) {
			params.put("flow_type", flow_type);
		}
		if (StringUtil.IsNotNullOrEmpty(water_type)) {
			params.put("water_type", water_type);
		}
		if (StringUtil.IsNotNullOrEmpty(station_id)) {
			params.put("station_id", station_id);
		}
		if (StringUtil.IsNotNullOrEmpty(build_status)) {
			params.put("build_status", build_status);
		}
		if(StringUtil.IsNotNullOrEmpty(station_name)){
			params.put("station_name", station_name);
		}
		if(StringUtil.IsNotNullOrEmpty(station_id_like)){
			params.put("station_id_like", station_id_like);
		}
		if(StringUtil.IsNotNullOrEmpty(companyCode)){
			params.put("companyCode", companyCode);
		}
		if(StringUtil.IsNotNullOrEmpty(stationStatus)){
			params.put("stationStatus", stationStatus);
		}
		if(StringUtil.IsNotNullOrEmpty(s_domaincode_like)){
			params.put("s_domaincode_like", s_domaincode_like);
		}

		return params;
	}

	// 获取污水厂所属监测站点(从监测点属性表获取)
	@RequestMapping(value = "/MonitorStationList")
	public void MonitorStationList(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String wwtpid = RequestUnit.getString("WWTP_ID");
		String sql = " SELECT * from BC_SITE_INFO WHERE WWTP_ID = @WWTP_ID ";

		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		GlobalParameter parm = new GlobalParameter("WWTP_ID", wwtpid);
		parms.add(parm);

		System.out.println(wwtpid);

		String result = DataUtil.getPageSelectJson(sql, parms);
		outString(result);
	}

	// 更新污水厂所属监测站点
	@RequestMapping(value = "/updateStation")
	public void updateStation(HttpServletResponse resq) {
		HashMap<String, Object> parms = RequestUnit.getHashMapParmO();
		String EXP_NO = parms.get("EXP_NO").toString();
		String sql = "select * from BC_SITE_INFO where EXP_NO='" + EXP_NO + "'";
		List<GlobalParameter> parm = new ArrayList<>();
		JSONObject list = DataUtil.getSelectJsonObject(sql, parm, null);
		String tableName = RequestUnit.getString("tableName");
		try{
			if (list.isEmpty()) {
				DataUtil.Create(tableName, parms);
			} else {
				DataUtil.Update(tableName, parms);
			}
			
			JSONObject json = new JSONObject();
			json.put("result", "success");
			outString(json.toString());
		}catch(Exception e){
			String msg = e.getMessage();
			outString(msg);
		}
	}

	@RequestMapping(value = "/updateWsclc")
	public void updateWsclc(HttpServletResponse resq) {
		HashMap<String, Object> parms = RequestUnit.getHashMapParmO();
		String EXP_NO = parms.get("SDEEXP_NO").toString();
		String sql = "select * from BC_ENTERPRISE_ATTR where EXP_NO='" + EXP_NO + "'";
		List<GlobalParameter> parm = new ArrayList<>();
		List<RowBase> list = DataUtil.getSelectRows(sql, parm);
		String tableName = RequestUnit.getString("tableName");
		try{
			if (list.isEmpty()) {
				DataUtil.Create(tableName, parms);
			} else {
				DataUtil.Update(tableName, parms);
			}

			JSONObject json = new JSONObject();
			json.put("result", "success");
			outString(json.toString());
		}catch(Exception e){
			String msg = e.getMessage();
			outString(msg);
		}
	}

	/**
	 * 查询污水处理厂坐标数据
	 */
	@RequestMapping(value = "/getGeoByid")
	public void getGeoBypeojectItem(HttpServletResponse res) {
		String id = RequestUnit.getParameter("EXP_NO");
		String sql = "select t2.*,sde.st_astext(t1.shape) wkt from SDE.PS_WWTPM t1 left join BC_ENTERPRISE_ATTR t2 on t1.exp_no=t2.exp_no where t1.EXP_NO='"
				+ id + "'";
		List<GlobalParameter> parms = new ArrayList<>();
		parms.add(new GlobalParameter("OBJECTID", id));

		JSONObject list = DataUtil.getSelectJsonObject(sql, parms, null);
		outString(list.toString());
	}

	/**
	 * 查询监测站点坐标数据
	 */
	@RequestMapping(value = "/getStationGeoByOBJID")
	public void getStationGeoByOBJID(HttpServletResponse res) {
		String OBJECTID = RequestUnit.getParameter("OBJECTID");
		String sql = "select t.*,sde.st_astext(t.shape) wkt from SDE.MONITOR_STATION t where t.OBJECTID="
				+ Integer.parseInt(OBJECTID);
		List<GlobalParameter> parms = new ArrayList<>();
		parms.add(new GlobalParameter("OBJECTID", OBJECTID));

		JSONObject list = DataUtil.getSelectJsonObject(sql, parms, null);
		outString(list.toString());
	}

	@RequestMapping(value = "/getStationGeoByExpNo")
	public void getStationGeoByExpNo(HttpServletResponse res) {
		String EXP_NO = RequestUnit.getParameter("EXP_NO");
		String sql = "select t.*,sde.st_astext(t.shape) wkt from SDE.MONITOR_STATION t where t.EXP_NO='" + EXP_NO + "'";
		List<GlobalParameter> parms = new ArrayList<>();
		parms.add(new GlobalParameter("EXP_NO", EXP_NO));

		JSONObject list = DataUtil.getSelectJsonObject(sql, parms, null);
		outString(list.toString());
	}

	/**
	 * 获取运营单位信息
	 * @throws Exception 
	 */
	@RequestMapping(value = "/getOperationUnit")
	public void getOperationUnit(HttpServletResponse res) throws Exception {
		String guid = RequestUnit.getParameter("GUID");
		Map<String,String> params = new HashMap<>();
		if(StringUtil.IsNotNullOrEmpty(guid)){
			params.put("GUID", guid);
		}
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = onlineMonitorService.getOperationUnit(params, null, pageInfo);
		JSONObject json = DataUtil.listToJSONObject(list, null, pageInfo);
		outString(json.toString());
	}
	
	/**
	 * 根据污水处理厂id获取运营单位信息
	 */
	@RequestMapping(value = "/getOperationUnitByStationID")
	public void getOperationUnitByStationID(HttpServletResponse res) {
		String station_id = RequestUnit.getParameter("STATION_ID");
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = onlineMonitorService.getOperationUnitByStationId(station_id);
		JSONObject json = DataUtil.listToJSONObject(list, null, pageInfo);
		outString(json.toString());
	}
	
	/**
	 * 根据污水处理厂id获取运营单位信息
	 */
	@RequestMapping(value = "/getOperationUnitByWWTPID")
	public void getOperationUnitByWWTPID(HttpServletResponse res) {
		String wwtp_id = RequestUnit.getParameter("WWTP_ID");
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = onlineMonitorService.getOperationUnitByWWTPID(wwtp_id);
		JSONObject json = DataUtil.listToJSONObject(list, null, pageInfo);
		outString(json.toString());
	}

	/**
	 * 获取数采仪信息
	 * 
	 * @throws Exception
	 * 
	 */
	@RequestMapping(value = "/getDataCollectionInstrument")
	public void getDataCollectionInstrument(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String wwtp_id = RequestUnit.getString("WWTP_ID");
		String station_id = RequestUnit.getString("STATION_ID");
		
		Map<String,String> params = new HashMap<>();
		if(StringUtil.IsNotNullOrEmpty(wwtp_id)){
			params.put("wwtp_id", wwtp_id);
		}
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}
		
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("BC_COLLECTION_INFO");
		
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = onlineMonitorService.getDataCollectionInstrument(params, pTable, pageInfo);
		JSONObject json = DataUtil.listToJSONObject(list, null, pageInfo);
		outString(json.toString());
	}

	/**
	 * 获取在线监测设备信息
	 * 
	 * @throws Exception
	 * 
	 */
	@RequestMapping(value = "/getMonitorEquipment")
	public void getMonitorEquipment(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String wwtp_id = RequestUnit.getString("WWTP_ID");
		String station_id = RequestUnit.getString("STATION_ID");
		
		Map<String,String> params = new HashMap<>();
		if(StringUtil.IsNotNullOrEmpty(wwtp_id)){
			params.put("wwtp_id", wwtp_id);
		}
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}
		
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("MONITOR_EQUIPMENT");
		
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = onlineMonitorService.getMonitorEquipment(params, pTable, pageInfo);
		JSONObject json = DataUtil.listToJSONObject(list, null, pageInfo);
		outString(json.toString());
	}

	/**
	 * 获取站点污染物
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "/getWryDataByExpNo")
	public void getWryDataByExpNo(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String wwtp_id = RequestUnit.getString("WWTP_ID");
		String station_id = RequestUnit.getString("STATION_ID");
		String deviceId = RequestUnit.getString("deviceId");
		String waterType = RequestUnit.getString("waterType");
		String pollCode = RequestUnit.getString("pollCode");
		
		Map<String,String> params = new HashMap<>();
		if(StringUtil.IsNotNullOrEmpty(wwtp_id)){
			params.put("wwtp_id", wwtp_id);
		}
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}
		if(StringUtil.IsNotNullOrEmpty(deviceId)){
			params.put("deviceId", deviceId);
		}
		if(StringUtil.IsNotNullOrEmpty(waterType)){
			params.put("waterType", waterType);
		}
		if(StringUtil.IsNotNullOrEmpty(pollCode)){
			params.put("pollCode", pollCode);
		}
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("BC_SITE_POLL");
		
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = onlineMonitorService.getStationPollutant(params, pTable, pageInfo);
		
		JSONObject json =  DataUtil.listToJSONObject(list, null, pageInfo);
		outString(json.toString());
	}
	/**
	 * 获取站点污染物短信报警设置信息
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "/getStationWryAlarmInfoByExpNo")
	public void getStationWryAlarmInfoByExpNo(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String wwtp_id = RequestUnit.getString("WWTP_ID");
		String station_id = RequestUnit.getString("STATION_ID");
		String deviceId = RequestUnit.getString("deviceId");
		String waterType = RequestUnit.getString("WATER_TYPE");
		String pollCode = RequestUnit.getString("pollCode");
		
		Map<String,String> params = new HashMap<>();
		if(StringUtil.IsNotNullOrEmpty(wwtp_id)){
			params.put("wwtp_id", wwtp_id);
		}
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}
		if(StringUtil.IsNotNullOrEmpty(deviceId)){
			params.put("deviceId", deviceId);
		}
		if(StringUtil.IsNotNullOrEmpty(waterType)){
			params.put("waterType", waterType);
		}
		if(StringUtil.IsNotNullOrEmpty(pollCode)){
			params.put("pollCode", pollCode);
		}
		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("BC_SITE_POLL");
		
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<List<RowBase>> list = onlineMonitorService.getStationPollAlarmThresholdSet(params, pTable, pageInfo);
		
		HashMap<String,Object> data=new HashMap<>();
		data.put("pollAlarmList", list);
		
		
		
		JSONObject json = DataUtil.objectToJsonObject(list);
		outString(json.toString());
	}
	
	/**
	 * 查询单条污染物的详情，包括污染物阈值集合
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "/getPollAlarmDetail")
	public void getPollAlarmDetail(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String station_id = RequestUnit.getString("STATION_ID");
		String pollCode = RequestUnit.getString("pollCode");
		
		Map<String,String> params = new HashMap<>();
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}
		if(StringUtil.IsNotNullOrEmpty(pollCode)){
			params.put("pollCode", pollCode);
		}
		List<RowBase> list = onlineMonitorService.getPollAlarmDetail(params);
		
		JSONObject json = DataUtil.objectToJsonObject(list);
		outString(json.toString());
	}
	
	/**
	 * 获取站点污染物,为报表左边栏参数选择服务
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "/getStationPollDict")
	public void getStationPollDict(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String station_id = RequestUnit.getString("STATION_ID");
		
		List<RowBase> list = onlineMonitorService.getStationPollDict(station_id);
		JSONObject json = DataUtil.listToJSONObject(list, null, null);
		outString(json.toString());
	}

	/**
	 * 获取设备验收数据
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getDeviceAcceptanceData")
	public void getDeviceAcceptanceData(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String wsclcid = RequestUnit.getString("WSCLC_ID");
		String stationCode = RequestUnit.getString("stationCode");
		Map<String,String> params = new HashMap<String,String>();
		if(StringUtil.IsNotNullOrEmpty(wsclcid)){
			params.put("wwtp_id", wsclcid);
		}
		if(StringUtil.IsNotNullOrEmpty(stationCode)){
			params.put("station_id", stationCode);
		}
		
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list  = onlineMonitorService.getStationDeviceWithAcceptanceInfo(params, pageInfo);
		JSONObject json = DataUtil.listToJSONObject(list, null, pageInfo);
		
		list = onlineMonitorService.getAcceptanceInfo(params, null);
		JSONArray acceptData = JSONArray.fromObject(list, JsonUtil.getConfig());
		json.put("acceptData", acceptData);
		String result = json.toString();
		outString(result);
		
	}

	/**
	 * 获取设备站点名称和代码列表
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getStationCodeList")
	public void getStationCode(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String wsclcid = RequestUnit.getString("WSCLC_ID");
		String station_id = RequestUnit.getString("stationCode");
		String flow_type = RequestUnit.getString("flowType");
		String station_category = RequestUnit.getString("stationType");
		List<GlobalParameter> params = new ArrayList<GlobalParameter>();
		
		String sql = "select distinct m.name,m.code,decode(k.status,null,'noaccept','1','acceptpass','2','acceptfail') as status from BC_SITE_INFO m left join"
				+ " (select * from (select d.wsclc_id,  d.station_code,  d.status,  d.accep_date,"
				+ " row_number() over(partition by d.wsclc_id, d.station_code order by d.accep_date desc) as rank"
				+ "  from BC_DEVICE_ACCE_INFO d) t where t.rank=1) k on m.wwtp_id=k.wsclc_id and m.code=k.station_code"
				+ "  where 1=1 ";
		
		if(StringUtil.IsNotNullOrEmpty(wsclcid)){
			sql+= " and m.wwtp_id=@WWTPID";
			params.add(new GlobalParameter("WWTPID", wsclcid));
		}
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			sql += " and m.code=@station_id";
			params.add(new GlobalParameter("station_id", station_id));
		}
		if(StringUtil.IsNotNullOrEmpty(flow_type)){
			sql += " and m.flow_type=@flow_type";
			params.add(new GlobalParameter("flow_type", flow_type));
		}
		if(StringUtil.IsNotNullOrEmpty(station_category)){
			sql += " and m.station_category=@station_category";
			params.add(new GlobalParameter("station_category", station_category));
		}
		
		String result = DataUtil.getSelectJson(sql, params, null);
		outString(result);
	}
	
	@RequestMapping(value = "/getReadTimeData")
	public void getReadTimeData(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String sql = "select t.name, t.code, d.device_num, r.parmname, r.rtd  from BC_SITE_INFO t  left join BC_COLLECTION_INFO d on t.code = d.site_id  left join  monitor_data_realtime r on d.device_num = r.mn where t.code = @station_id";
		String station_id = RequestUnit.getString("STATION_ID");
		List<GlobalParameter> params = new ArrayList<GlobalParameter>();
		params.add(new GlobalParameter("station_id", station_id));
		String result = DataUtil.getSelectJson(sql, params, null);
		outString(result);
	}
	
	
	/**
	 * 查询站点在线离线信息
	 */
	@RequestMapping(value = "/getStationStatus")
	public void getStationStatus(HttpServletResponse res) {
		
		String sql = "select ms.code,dc.status from BC_SITE_INFO ms left join BC_COLLECTION_INFO dc on ms.code = dc.site_id where 1=1";
		
		List<GlobalParameter> parms = new ArrayList<>();
		String station_name = RequestUnit.getParameter("STATION_NAME");
		if(StringUtil.IsNotNullOrEmpty(station_name)){
			sql+= " and ms.name like '%"+station_name+"%'";
		}
		String station_code = RequestUnit.getParameter("STATION_CODE");
		if(StringUtil.IsNotNullOrEmpty(station_code)){
			sql+= " and ms.code like '__"+station_code+"%'";
			parms.add(new GlobalParameter("station_code", station_code));
		}

		JSONObject list = DataUtil.getSelectJsonObject(sql, parms, null);
		outString(list.toString());
	}
	
	/**
	 * 获取污水处理厂详情
	 */
	@RequestMapping(value = "/getWWTPDetailByID")
	public void getWWTPDetailByID(HttpServletRequest resq, HttpServletResponse res) {
		String sql = "select * from BC_ENTERPRISE_ATTR t where t.exp_no = @code";
		List<GlobalParameter> parms = new ArrayList<>();
		String code = RequestUnit.getParameter("CODE");
		parms.add(new GlobalParameter("code", code));
		JSONObject list = DataUtil.getSelectJsonObject(sql, parms, null);
		outString(list.toString());
	}
	
	/**
	 * 获取 站点详情
	 * @throws Exception 
	 */
	@RequestMapping(value = "/getStationRealTimeData")
	public void getStationRealTimeData(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		JSONObject json = new JSONObject();
		
		String sql = "select PHOTO from BC_SITE_INFO t where t.code = @code";
		List<GlobalParameter> parms = new ArrayList<>();
		String code = RequestUnit.getParameter("CODE");
		String mn = RequestUnit.getParameter("mn");
		parms.add(new GlobalParameter("code", code));
		
		Object image = DataUtil.getScale(sql, parms);
		if(image!=null){
			json.put("IMG", image.toString());
		}
		
		HashMap<String, RowBase> rows = remoteService.getStationRealTimeData(mn);
		if(rows!=null && rows.size()>0){
			JSONArray arr = JSONArray.fromObject(rows,JsonUtil.getConfig());
			json.put("data", arr);
		}
		
		outString(json.toString());
	}
	
	/**
	 * 获取 站点详情
	 */
	@RequestMapping(value = "/getStationDetailByID")
	public void getStationDetailByID(HttpServletRequest resq, HttpServletResponse res) {
		String sql = "select * from BC_SITE_INFO t where t.code = @code";
		List<GlobalParameter> parms = new ArrayList<>();
		String code = RequestUnit.getParameter("CODE");
		parms.add(new GlobalParameter("code", code));
		JSONObject json = DataUtil.getSelectJsonObject(sql, parms, null);
		outString(json.toString());
	}
	
	/**
	 * 获取设备生命周期数据
	 * @throws Exception 
	 */
	@RequestMapping(value = "/getDeviceLifeCycleInfo")
	public void getDeviceLifeCycleInfo(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String,String> params = new HashMap<String,String>();
		String wwtp_id = RequestUnit.getParameter("WWTP_ID");
		if(StringUtil.IsNotNullOrEmpty(wwtp_id)){
			params.put("wwtp_id", wwtp_id);
		}
		String flow_type = RequestUnit.getParameter("FLOW_TYPE");
		if(StringUtil.IsNotNullOrEmpty(flow_type)){
			params.put("flow_type", flow_type);
		}
		String station_id = RequestUnit.getParameter("STATION_ID");
		if(StringUtil.IsNotNullOrEmpty(station_id)){
			params.put("station_id", station_id);
		}
		String is_oos = RequestUnit.getParameter("IS_OOS");
		if(StringUtil.IsNotNullOrEmpty(is_oos)){
			params.put("is_oos", is_oos);
		}
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = onlineMonitorService.getDeviceLifeCycleInfo(params, pageInfo);
		JSONObject json = DataUtil.listToJSONObject(list, null, pageInfo);
		outString(json.toString());
	}
	/**
	 * 获取视频信息
	 * @throws Exception 
	 */
	@RequestMapping(value = "/getVideoData")
	public void getVideoData(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String wwtp_id = RequestUnit.getParameter("WWTP_ID");
		String station_id = RequestUnit.getParameter("STATION_ID");
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = onlineMonitorService.getVideoDataByWWTPID(wwtp_id,station_id);
		JSONObject json = DataUtil.listToJSONObject(list, null, pageInfo);
		outString(json.toString());
		
	}
	/**
	 * 根据数目获取视频数据
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="/getVideoDataBynum")
	public void getVideoDataBynum(HttpServletRequest resq, HttpServletResponse res) throws Exception{
		String num = RequestUnit.getParameter("num");
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = onlineMonitorService.getVideoDataByNum(num);
		JSONObject json = DataUtil.listToJSONObject(list, null, pageInfo);
		outString(json.toString());
	}
	
	@RequestMapping(value="/getPollutantsByDeviceId")
	public void getPollutantsByDeviceId(HttpServletRequest resq, HttpServletResponse res){
		String deviceId = RequestUnit.getParameter("deviceId");
		List<RowBase> rows = onlineMonitorService.getPollutantsByDeviceId(deviceId);
		JSONObject json = DataUtil.listToJSONObject(rows, null, null);
		outString(json.toString());
	}
	/**
	 * 获取DVR信息
	 * @throws Exception 
	 */
	@RequestMapping(value = "/getDVRData")
	public void getDVRData(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String wwtp_id = RequestUnit.getParameter("WWTP_ID");
		String station_id = RequestUnit.getParameter("STATION_ID");
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = onlineMonitorService.getDVRDataByWWTPID(wwtp_id,station_id);
		JSONObject json = DataUtil.listToJSONObject(list, null, pageInfo);
		outString(json.toString());
		
	}
	/**
	 * 根据阈值GUID查询相应的角色和用户，并包括所有的角色和用户
	 */
	@RequestMapping(value = "/getOldAndAllRoleAndUser")
	public void queryOldRoleAndUser(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String GUID = RequestUnit.getParameter("GUID");
		String Mn = RequestUnit.getParameter("GUMnID");
		String pollCode = RequestUnit.getParameter("POLLUTE_CODE");
		List<List<RowBase>> result = onlineMonitorService.queryOldRoleAndUser(GUID,Mn,pollCode);
		JSONObject json = DataUtil.objectToJsonObject(result);
		outString(json.toString());
	}
	/**
	 * 分别查询所有的角色和用户
	 */
	@RequestMapping(value = "/getAlarmRoleAndUserSet")
	public void getAlarmRoleAndUserSet(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String GUID = RequestUnit.getParameter("alarm_guid");
		List<List<RowBase>> result = onlineMonitorService.getAlarmRoleAndUserSet(GUID);
		JSONObject json = DataUtil.objectToJsonObject(result);
		outString(json.toString());
	}
	/**
	 * 根据rold获取角色和用户
	 */
	@RequestMapping(value = "/getTheUserOfRole")
	public void getTheUserOfRole(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String roleId = RequestUnit.getParameter("role");
		List<RowBase> result = onlineMonitorService.queryUserOfRole(roleId);
		JSONObject json = DataUtil.listToJSONObject(result);
		outString(json.toString());
	}
	/**
	 * 根据GUID删除阈值数据以及角色、用户数据
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="/deletePollAlarmByGUID")
	public void deletePollAlarmByGUID(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String GUID = RequestUnit.getParameter("GUID");
		onlineMonitorService.deletePollAlarmByGUID(GUID);
	}
	
	
	/**
	 * 保存站点污染物报警设置数据
	 */
	@RequestMapping(value="/savePollAlarmInfo")
	public void saveAndUpdatePollAlarmInfo(HttpServletRequest resq, HttpServletResponse res)throws Exception {
		HashMap<String, String> viewValues = RequestUnit.getHashMapParmS();
		onlineMonitorService.saveAndUpdatePollAlarmInfo(viewValues);
	}
}
